package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.nonIntelligentDevice.vo.RoomDevicePriceVO;
import com.xiaoshuidi.cloud.module.rooms.enums.DevicePriceType;
import com.xiaoshuidi.cloud.module.rooms.enums.DeviceTypeEnum;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomDevicePriceAssociationMapper;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomDevicePriceMapper;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomSdiotReadingOrderMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomDevicePrice;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomDevicePriceAssociation;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotReadingOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: lease-module-rooms
 * @BelongsPackage: com.xiaoshuidi.cloud.module.rooms.repository
 * @Author: James
 * @CreateTime: 2023-06-07  14:48
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class IotRoomDevicePriceRepository extends ServiceImpl<RoomDevicePriceMapper, RoomDevicePrice> {
    @Autowired
    RoomDevicePriceMapper roomDevicePriceMapper;

    @Autowired
    RoomDevicePriceAssociationMapper roomDevicePriceAssociationMapper;

    @Autowired
    RoomSdiotReadingOrderMapper roomSdiotReadingOrderMapper;

    public void addConfig(RoomDevicePrice roomDevicePrice) {
        roomDevicePriceMapper.insert(roomDevicePrice);
    }

    public PageResult<RoomDevicePriceVO> findAll(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<RoomDevicePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", DevicePriceType.DEVICE_PRICE_UNSTEP.getCode());
        List<RoomDevicePrice> roomDevicePrices = roomDevicePriceMapper.selectList(queryWrapper);
        List<RoomDevicePriceVO> listVo = new ArrayList<>();
        for (RoomDevicePrice roomDevicePrice : roomDevicePrices) {
            Long id = roomDevicePrice.getId();
            QueryWrapper<RoomDevicePriceAssociation> qw = new QueryWrapper<>();
            qw.eq("device_price_id", id);
            // todo @黄光辉 查询数据量用count查询, 不要查所有数据
            List<RoomDevicePriceAssociation> list = roomDevicePriceAssociationMapper.selectList(qw);
            int size = list.size();
            RoomDevicePriceVO roomDevicePriceVO = new RoomDevicePriceVO();
            roomDevicePriceVO.setId(roomDevicePrice.getId());
            roomDevicePriceVO.setBz(roomDevicePrice.getBz());
            roomDevicePriceVO.setElectricityPrice(roomDevicePrice.getElectricityPrice());
            roomDevicePriceVO.setColdWorterPrice(roomDevicePrice.getColdWorterPrice());
            roomDevicePriceVO.setHotWorterPrice(roomDevicePrice.getHotWorterPrice());
            roomDevicePriceVO.setPngPrice(roomDevicePrice.getPngPrice());
            roomDevicePriceVO.setSize(size);
            roomDevicePriceVO.setCreateTime(roomDevicePrice.getCreateTime());
            listVo.add(roomDevicePriceVO);
        }
        PageResult<RoomDevicePriceVO> pageResult = new PageResult<>();
        pageResult.setList(listVo);
        pageResult.setTotal((long) listVo.size());
        return pageResult;
    }

    public CommonResult<String> deleteConfig(Long devicePriceId) {
        QueryWrapper<RoomDevicePriceAssociation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_price_id", devicePriceId);
        // todo @黄光辉 查询数据量用count查询, 不要查所有数据
        List<RoomDevicePriceAssociation> roomDevicePriceAssociations = roomDevicePriceAssociationMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(roomDevicePriceAssociations)) {
            return CommonResult.success("此单价设置已应用到房源，不可删除！");
        }
        roomDevicePriceMapper.deleteById(devicePriceId);
        return CommonResult.success("此单价设置已删除");
    }

    public RoomDevicePrice getDeviceByRoomId(Long roomId) {
        QueryWrapper<RoomDevicePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rsdpa.room_id", roomId);
        queryWrapper.eq("rsdp.type", DevicePriceType.DEVICE_PRICE_UNSTEP.getCode());
        queryWrapper.eq("rsdp.deleted", 0);
        queryWrapper.eq("rsdpa.deleted", 0);
        return roomDevicePriceMapper.getDeviceByRoomId(queryWrapper);
    }

    public void updateConfig(RoomDevicePrice roomDevicePrice) {
        //todo @黄光辉 事务; 批量插入不要在循环中操作数据库
        roomDevicePriceMapper.updateById(roomDevicePrice);
        Long id = roomDevicePrice.getId();
        QueryWrapper<RoomDevicePriceAssociation> qw = new QueryWrapper<>();
        qw.eq("device_price_id", id);
        List<RoomDevicePriceAssociation> list = roomDevicePriceAssociationMapper.selectList(qw);
        for (RoomDevicePriceAssociation priceAssociation : list) {
            Long roomId = priceAssociation.getRoomId();
            QueryWrapper<RoomSdiotReadingOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("room_house_id", roomId);
            List<RoomSdiotReadingOrder> orders = roomSdiotReadingOrderMapper.selectList(queryWrapper);
            for (RoomSdiotReadingOrder order : orders) {
                if (DeviceTypeEnum.COLDWATER_FEE_TYPE.getName().equals(order.getFeeType())) {
                    order.setPrice(new BigDecimal(Double.toString(roomDevicePrice.getColdWorterPrice())));
                } else if (DeviceTypeEnum.HOTWATER_FEE_TYPE.getName().equals(order.getFeeType())) {
                    order.setPrice(new BigDecimal(Double.toString(roomDevicePrice.getHotWorterPrice())));
                } else if (DeviceTypeEnum.AMMETER_FEE_TYPE.getName().equals(order.getFeeType())) {
                    order.setPrice(new BigDecimal(Double.toString(roomDevicePrice.getElectricityPrice())));
                }
                roomSdiotReadingOrderMapper.updateById(order);
            }
        }
    }

}
